/**
 * 543. 二叉树的直径
 */
public class Solution_543 {
    int ans = 0;

    public int diameterOfBinaryTree(TreeNode root) {
        depth(root);
        return ans;
    }

    public int depth(TreeNode node) {
        if (node == null) {
            return 0;
        }
        int leftDepth = depth(node.left);
        int rightDepth = depth(node.right);
        ans = Math.max(ans, leftDepth + rightDepth);
        // 返回以此节点为根的树的深度
        return Math.max(leftDepth, rightDepth) + 1;
    }
}
